package com.orhon.smartcampus.modules.base.fetcher;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.dao.RegionsDao;
import com.orhon.smartcampus.modules.base.entity.Regions;
import com.orhon.smartcampus.modules.base.gqlModel.GQLRegion;
import com.orhon.smartcampus.modules.base.mm.RegionMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class RegionFetcher implements DataFetcher {

    @Autowired
    RegionsDao regionsDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Regions where deleted_at = null and id = "+id;
        Regions item =  regionsDao.findId(hql, null);
        
        GQLRegion parGQLRegion = null;
		if (env.getSelectionSet().contains("parent")){
			Regions regions = item.getParentRegions();
			parGQLRegion = RegionMapper.MAPPER.toRegions(regions);
		}
        
        GQLRegion gqlRegion = RegionMapper.MAPPER.toRegions(item);
        gqlRegion.setParent(parGQLRegion);
        return gqlRegion;
    }


}
